package servlet;

import pojo.Dept;
import service.DeptService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(urlPatterns = "/toEditDept")
public class ToEditDeptServlet extends HttpServlet {
    // alt + insert

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 3行固定代码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        // 接收参数
        String str_id = req.getParameter("id");
        // 把字符串的id转换为整数类型
        int id = Integer.parseInt(str_id);
        // 创建业务层对象
        DeptService deptService = new DeptService();
        // 根据id查询单个用户对象
        Dept dept = deptService.getDeptById(id);

        // 把对象存在作用域中
        req.getSession().setAttribute("dept",dept);

        // 跳转到编辑页面
        req.getRequestDispatcher("deptedit.jsp").forward(req,resp);
    }
}
